function sendMessage() {
	var message = readChatMessage();
	if (message === "")
		return;

	$.ajax({
		url : getContextPath() + "/Chat",
		type : "POST",
		data : {
			groupId : currentGroupId,
			email : applicationContext.email,
			accessToken : applicationContext.accessToken,
			text : message,
			messageCounter: currentMsgHead
		},
		success : function(response) {
			$.each(response.messages, function(index, msg) {
				handleChatMessage(msg);
			});
			currentMsgHead = response.messageCounter;
		}

	});  
}

function readChatMessage() {
	var message = $("#chatMessage").val();
	$("#chatMessage").val("");

	return message;
}

function handleChatMessage(msg) {
	var currentDate = $.datepicker.formatDate("dd-mm-yy", new Date(msg.date));
	var timeContainer = $("<div class='timeContainer'></div>");
	timeContainer.append(currentDate);
	timeContainer.append(" -- " + msg.owner);

	var bubbleContainer = $("<div class='bubble'></div");
	bubbleContainer.append(constructChatBubble(msg.text));
	bubbleContainer.append(timeContainer);

	$("#chatMessageList").append(bubbleContainer);

	$("#chatMessageList").scrollTop($("#chatMessageList").prop("scrollHeight"));
}

function constructChatBubble(message) {
	var chatBubble = $("<div class='triangle-isosceles'></div>");
	chatBubble.append(message);
	return chatBubble;
}

function pollChatMessages() {
	chatPollId = setTimeout(function () {
    	$.ajax({
			url : getContextPath() + "/Chat",
			type: "GET",
			data: {
				groupId : currentGroupId,
				messageCounter: currentMsgHead,
				email: applicationContext.email,
				accessToken: applicationContext.accessToken
			},
			complete: pollChatMessages,
			success : function(response) {
				if (response.success) {
					$.each(response.messages, function(index, msg) {
						handleChatMessage(msg);
					});
					currentMsgHead = response.messageCounter;
				}
			}
		});
    }, 5000);
}

function pollGroupList() {
   	$.ajax({
		url : getContextPath() + "/Group",
		type: "GET",
		data: {
			action: "myGroup",
			email: applicationContext.email,
			accessToken: applicationContext.accessToken
		},
		success : function(response) {
			if (response.success) {
				var groupList = $("#groupList");
				groupList.empty();
				$.each(response.memberships, function(index, group) {
					var aGroup = $("<li></li>").attr("data-groupId", group.groupId).attr("data-groupRole", group.role).html($("<span></span>").html(group.groupName)).appendTo(groupList);
					$("<button>Enter</button>").button().addClass("enterGroupChat").appendTo(aGroup);
					if (group.role === "owner") {
						$("<button>Delete</button>").button().addClass("deleteGroup").appendTo(aGroup);
					}
				});
			}
		}
	});
}

function getMemberList() {
	$.ajax({
		url : getContextPath() + "/Group",
		type: "GET",
		data: {
			action: "viewGroupMember",
			groupId: currentGroupId, 
			email: applicationContext.email,
			accessToken: applicationContext.accessToken
		},
		success : function(response) {
			if (response.success) {
				var memberList = $("#memberList");
				memberList.empty();
				
				var isOwner = false;
				$.each(response.memberships, function(index, member) {			
					if (member.email === applicationContext.email && member.role === "owner") {
						isOwner = true;
					}
				});
				
				$.each(response.memberships, function(index, member) {
					var memberInfo = $("<li></li>").html(member.email).appendTo(memberList);
					if (isOwner) {
						if (member.role !== "owner") {
							$("<button>Kick</button>").attr("data-email", member.email).button().addClass("kickUser").appendTo(memberInfo);
						}
					} else {
						if (member.role !== "owner") {
							$("<button>Leave</button>").attr("data-email", member.email).button().addClass("kickUser").appendTo(memberInfo);
						}
					}
				});
			}
		}
	});
}

function createGroup(groupName) {
	$.ajax({
		url : getContextPath() + "/Group",
		type: "POST",
		data: {
			action: "createGroup",
			email: applicationContext.email,
			accessToken: applicationContext.accessToken,
			groupName: groupName
		},
		success : function(response) {
			if (response.success) {
				pollGroupList();
			} else {
				showErrorMessage(response.errorMessage);
			}
		}
	});
}